package cn.keepme.common.consts;

import java.io.BufferedReader;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 常用工具函数集合
 * 
 * @version 1.0 2015-7-20
 * @author Wu.Pan (wupan@10086.im)
 */
public class PublicOPCode {

	private static Log log = LogFactory.getLog(PublicOPCode.class);
	
	public static Vector<String> readAllowips(String location) {
		log.debug("readAllowips from " + location);
		String tmp;
		Vector<String> allowips = new Vector<String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(location));
			while ((tmp = br.readLine()) != null) {
				allowips.add(tmp.trim());
			}
		} catch (Exception e) {
			log.error("error occurs", e);
		}
		return allowips;
	}
	

	
	/**
	 * 15位唯一标识
	 */
	public static String getSerialTime15() {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(new Date().getTime());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddhhmmssSSS");
		String s = dateFormat.format(c.getTime()).substring(0,15);
		return s;
	}
	
	/**
	 * 12位唯一标识
	 */
	public static String getSerialTime12() {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(new Date().getTime());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddhhmmssSSS");
		String s = dateFormat.format(c.getTime()).substring(1,13);
		return s;
	}
	
	public static void main(String[] args){
		System.out.println(getSerialTime15());
		System.out.println(getSerialTime12());
	}
}
